package com.ruoyi.secondvitim.service.impl;

import java.util.List;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.secondvitim.domain.QueFormDetail;
import com.ruoyi.secondvitim.domain.QueFormDetailOption;
import com.ruoyi.secondvitim.mapper.QueFormDetailMapper;
import com.ruoyi.secondvitim.service.IQueFormDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;

/**
 * 问卷细项Service业务层处理
 * 
 * @author alonso_ecit
 * @date 2021-12-17
 */
@Service
public class QueFormDetailServiceImpl implements IQueFormDetailService
{
    @Autowired
    private QueFormDetailMapper queFormDetailMapper;

    /**
     * 查询问卷细项
     *
     * @param id 问卷细项ID
     * @return 问卷细项
     */
    @Override
    public QueFormDetail selectQueFormDetailById(Long id)
    {
        return queFormDetailMapper.selectQueFormDetailById(id);
    }

    /**
     * 查询问卷细项列表
     * 
     * @param queFormDetail 问卷细项
     * @return 问卷细项
     */
    @Override
    public List<QueFormDetail> selectQueFormDetailList(QueFormDetail queFormDetail)
    {
        return queFormDetailMapper.selectQueFormDetailList(queFormDetail);
    }

    @Override
    public List<QueFormDetailOption> selectQueFormDetailOptionList(QueFormDetailOption queFormDetailOption) {
        return queFormDetailMapper.selectQueFormDetailOptionList(queFormDetailOption);
    }

    /**
     * 新增问卷细项
     * 
     * @param queFormDetail 问卷细项
     * @return 结果
     */
    @Override
    public int insertQueFormDetail(QueFormDetail queFormDetail)
    {
        queFormDetail.setCreateTime(DateUtils.getNowDate());
        int rows = queFormDetailMapper.insertQueFormDetail(queFormDetail);
        insertQueFormDetailOption(queFormDetail);
        return rows;
    }

    /**
     * 修改问卷细项
     * 
     * @param queFormDetail 问卷细项
     * @return 结果
     */
    @Override
    public int updateQueFormDetail(QueFormDetail queFormDetail)
    {
        queFormDetail.setUpdateTime(DateUtils.getNowDate());
        queFormDetailMapper.deleteQueFormDetailOptionByFormDetailId(queFormDetail.getId());
        insertQueFormDetailOption(queFormDetail);
        return queFormDetailMapper.updateQueFormDetail(queFormDetail);
    }

    /**
     * 更高选择状态
     * */
    @Override
    public void updateQueFormDetailStatus(QueFormDetail queFormDetail){
        queFormDetail.setUpdateTime(DateUtils.getNowDate());
        queFormDetailMapper.updateQueFormDetail(queFormDetail);
    }

    @Override
    public List<QueFormDetail> selectQueSubmitPreviewFormDetail(Long id) {
        return queFormDetailMapper.selectQueSubmitPreviewFormDetail(id);
    }

    /**
     * 删除问卷细项对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteQueFormDetailByIds(Long[] ids)
    {
        queFormDetailMapper.deleteQueFormDetailOptionByFormDetailIds(ids);
        return queFormDetailMapper.deleteQueFormDetailByIds(ids);
    }

    /**
     * 删除问卷细项信息
     * 
     * @param id 问卷细项ID
     * @return 结果
     */
    @Override
    public int deleteQueFormDetailById(Long id)
    {
        queFormDetailMapper.deleteQueFormDetailOptionByFormDetailId(id);
        return queFormDetailMapper.deleteQueFormDetailById(id);
    }

    /**
     * 新增单选项信息
     * 
     * @param queFormDetail 问卷细项对象
     */
    public void insertQueFormDetailOption(QueFormDetail queFormDetail)
    {
        List<QueFormDetailOption> queFormDetailOptionList = queFormDetail.getQueFormDetailOptionList();
        Long id = queFormDetail.getId();
        if (StringUtils.isNotNull(queFormDetailOptionList))
        {
            List<QueFormDetailOption> list = new ArrayList<QueFormDetailOption>();
            for (QueFormDetailOption queFormDetailOption : queFormDetailOptionList)
            {
                queFormDetailOption.setFormDetailId(id);
                list.add(queFormDetailOption);
            }
            if (list.size() > 0)
            {
                queFormDetailMapper.batchQueFormDetailOption(list);
            }
        }
    }
}
